package com.supermap.dataservice.disaster.dao;

import com.supermap.dubbo.api.disaster.entity.Disaster;
import com.supermap.dubbo.api.disaster.entity.DisasterComment;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:灾情数据访问层
 * @Date:18/1/29 下午2:42
 */
@Mapper
public interface DisasterDao {
    /**
     * 获得用户的所有灾情信息
     * @return
     */
    @Select("select t3.* ,t4.name as cropname from (select t1.*,t2.username from t_disaster t1 \\n\" +\n" +
            "            \"inner JOIN t_user t2 on t1.userId=t2.id) t3\\n\" +\n" +
            "            \"inner JOIN t_crop t4 on t3.type=t4.id where IF(ISNULL(#{userId}),true,t3.userId=#{userId}) and IF(ISNULL(#{areaCode}),true,t3.areacode like #{areaCode})")
    public List<Disaster> getDisasterByUIdArea(@Param("userId") String userId, @Param("areaCode") String areaCode);

    /**
     *增加灾情
     * @param disaster
     */
    @Insert("insert into t_disaster(userId,farm,type,breed,growthperiod,longitude,latitude,areacode,descript,img_url,disastertype,development)\n " +
            "values(#{disaster.userId},#{disaster.farm},#{disaster.type},#{disaster.breed},#{disaster.growthperiod},\n" +
            "#{disaster.longitude},#{disaster.latitude},#{disaster.areacode},#{disaster.descript},#{disaster.imgUrl},\n" +
            "#{disaster.disastertype},#{disaster.development})")
    public void addDisaster(@Param("disaster") Disaster disaster);


    /**
     * 获取所有的灾情信息
     * @return
     */
    @Select("select t3.* ,t4.`name` as cropname from (select t1.*,t2.username from t_disaster t1 \n" +
            "Inner JOIN t_user t2 on t1.userId=t2.id) t3\n" +
            "Inner JOIN t_crop t4 on t3.type=t4.id;")
    public List<Disaster> getAllDisaster();

    /**
     * 获取所有的灾情评论
     * @return
     */
    @Select("select * from t_disastercomment")
    public List<DisasterComment> getAllDisasterComment();

    /**
     * 删除灾情
     * @param disId
     */
    @Delete("Delete from t_disaster where id=#{disId}")
    public void delDisaster(@Param("disId") int disId);

    /**
     * 根据区域编码获取所有的灾情信息
     * @return
     */
    @Select("select t3.* ,t4.`name` as cropname,t5.name as disasterName from (select t1.*,t2.username from t_disaster t1 " +
            " Inner JOIN t_user t2 on t1.userId=t2.id) t3 " +
            " Inner JOIN t_crop t4 on t3.type=t4.id " +
            " Inner JOIN t_disastertype t5 on t3.disastertype=t5.code " +
            " where t3.areacode =#{areacode}")

    public List<Disaster> getDisasterByareacode(@Param("areacode") String areacode);

    /**
     * 根据type得到农情
     * @param type
     * @return
     */
    @Select("select t1.*,t2.username as username,t3.areaname as areaname from t_disaster t1 left join t_user t2 on t1.userId = t2.id left join t_area t3 on t3.areacode = t1.areacode where if((isnull(#{type}) or #{type}=''),true,t1.type in(select id from t_crop where id in(select id from t_crop where parentid = #{type} or id= #{type}) or"
            +" parentid in (select id from t_crop where parentid = #{type} ))) and if(isnull(#{areaCode}),true,t1.areacode like CONCAT(#{areaCode},'%'))  and if(isnull(#{disasterType}) or #{disasterType}='',true,t1.disastertype = #{disasterType}) and if(isnull(#{dateTime}),true,t1.createTime like CONCAT(#{dateTime},'%'))")
    List<Disaster> getDisasters(@Param("type") String type, @Param("areaCode") String areaCode, @Param("disasterType") String disasterType, @Param("dateTime") String dateTime);

    /**
     * 搜索
     * @param showName
     * @param areaCode
     * @return
     */
    @Select("select t1.*,t2.username as username,t2.showName,t3.areaname as areaname " +
            "from t_disaster t1 left join t_user t2 on t1.userId = t2.id left join t_area t3 on t3.areacode = t1.areacode where t2.showName like #{showName} \n" +
            "and t2.areaCode like #{areaCode}")
    List<Disaster> searchDisasterByShowName(@Param("showName") String showName, @Param("areaCode") String areaCode);
}
